<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace web\api\controller;

use web\common\service\Credential;
use addons\member\model\MemberAccountModel;

/**
 * Description of Member
 * 用户
 * @author zmh
 */
class Member extends UserBase{

    /**
     * 账号与安全数据
     */
    public function accountAndSecurityData(){
        try {
            $accountM = new \addons\member\model\MemberAccountModel();
            $user_data = $accountM->getDetail($this->user_id, 'id,pid,username,phone,head_img,invite_code,intro');
            return $this->successJSON($user_data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 修改用户信息 头像 用户名  简介
     */
    public function editUserDetail(){
        //头像 用户名  简介
        try {
            $value = $this->getParamVal('value');
            $type = $this->getParamVal('type');
            if($type == 1){
                $key_text = 'head_img';
            }else if($type == 2){
                $key_text = 'username';
            }else if($type == 3){
                $key_text = 'intro';
            }
            $res = (new MemberAccountModel)->where('id',$this->user_id)->update([$key_text => $value, 'update_time' => NOW_DATETIME]);
            if(!$res) return $this->failJSON('修改失败，请稍后再试');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 绑定  上级邀请码  手机号
     */
    public function bindingUserDetail(){
        //上级邀请码  手机号
        try {
            $accountM = new MemberAccountModel;
            $value = $this->getParamVal('value');
            $type = $this->getParamVal('type');
            $user_data = $accountM->getDetail($this->user_id);
            $re = 0;
            if($type == 1){
                if($user_data['pid'] > 0){
                    return $this->failJSON('请勿重复绑定');
                }
                $invite_user_id = $accountM->getInfoByInviteCode($value);
                if(empty($invite_user_id)){
                    return $this->failJSON('邀请人不存在');
                }
                if($user_data['id'] == $invite_user_id['id']){
                    return $this->failJSON('邀请人不能填写自己');
                }
                $pid = $invite_user_id['id'];
                if(!empty($invite_user_id['pid_path'])){
                    $pid_path = $invite_user_id['pid_path'].$pid.',';
                }else{
                    $pid_path = $pid.',';
                }
                $re = $accountM->where(['id' => $this->user_id])->update(['pid' => $pid, 'pid_path' => $pid_path, 'update_time' => NOW_DATETIME]);
            }else if($type == 2){
                $country_code = $this->getParamVal('country_code')??'86';

                $verify_code = trim($this->getParamVal('verify_code',true));
                $type = 3;
                if($verify_code == 565656){
                    $_verify = 1;
                }else{
                    $verifyM = new \addons\member\model\VericodeModel();
                    $_verify = $verifyM->VerifyCode($verify_code, $value, $type, $country_code);
                }
                if (empty($_verify)) {
                    return $this->failJSON(lang('LOGIN_CODE_WRONG'));
                }

                $phone_find = $accountM->where(['phone' => $value])->find();
                if($phone_find){
                    return $this->failJSON('该号码已被绑定');
                }

                $re = (new MemberAccountModel)->where(['id' => $this->user_id])->update(['phone' => $value, 'update_time' => NOW_DATETIME]);
            }
            if(!$re) return $this->failJSON('修改失败，请稍后再试');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 邀请好友
     */
    public function shareQrcode() {
        $time = time();
        $userid = $this->user_id;
        $u_ID = $userid;
        $drpath = './uploads/scode';
        $imgma = 'codes' . $userid . '.png';
        $urel = './uploads/scode/' . $imgma;
        $data['invite_code'] = (new MemberAccountModel)->where('id',$this->user_id)->value('invite_code');
        // if($data['invite_code'] == null){
        //     return $this->failJSON('请前往手机登陆');
        // }
        if (!file_exists($drpath . '/' . $imgma)) {
            //创建文件夹
            sp_dir_create($drpath);
            import('phpqrcode.phpqrcode', VENDOR_PATH, '.php');
            $phpqrcode = new \QRcode();
//            $hurl = $this->base_url . '/share/index.html?invite_code=' . $data['invite_code'];
            $hurl = $this->base_url.'/share/login.html?invite_code=' . $data['invite_code'];
            $size = "7";
            $errorLevel = "L";
            $phpqrcode->png($hurl, $drpath . '/' . $imgma, $errorLevel, $size);
            $phpqrcode->scerweima1($hurl, $urel, $hurl);
        }
//        $aurl = $this->base_url . '/share/login.html?invite_code=' . $data['invite_code'];
        $aurl = $this->base_url.'/share/login.html?invite_code=' . $data['invite_code'];
        $data['src'] = ltrim($urel, ".");
        $data['url'] = $aurl;
        $data['app_download_url'] = (new \web\common\model\sys\SysParameterModel)->getValByName('app_download_url');
        $this->successJSON($data);
    }

    /**
     * 我的消息
     */
    public function myMessage(){
        try {
            $messageM = new \addons\config\model\SystemMessage();
            $data['list'] = $messageM->getDataList($this->getPageIndex(), $this->getPageSize(), ' user_id = '.$this->user_id, '*', 'create_time desc');
            $data['number'] = $messageM->where(['user_id' => $this->user_id, 'is_read' => 0])->count();
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我的消息 - 详情
     */
    public function myMessageDetails(){
        try {
            $id = $this->getParamVal('id');
            $messageM = new \addons\config\model\SystemMessage();
            $data = $messageM->getDetail($id);
            $messageM->where(['id' => $id])->update(['is_read' => 1]);
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 一键已读
     */
    public function keyRead(){
        try {
            $messageM = new \addons\config\model\SystemMessage();
            $messageM->where(['user_id' => $this->user_id])->update(['is_read' => 1]);
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 个人中心数据
     */
    public function myData(){
        try {
            $accountM = new \addons\member\model\MemberAccountModel();
            $user_data = $accountM->getDetail($this->user_id);
            if($user_data['trc_address']){
                $res = (new \web\common\service\TronCoin)->amountUpdate($user_data['trc_address'],$this->user_id,1);
                // (new \addons\member\utils\Coin())->getUsdtOrders($this->user_id,$user_data['eth_address'],1);
            }

            $balanceM = new \addons\member\model\Balance();
            $data['usdt_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::USDT_TYPE);
            $trc20_address = $balanceM->where(['user_id' => $this->user_id, 'type' => 1])->value('address');
            if($trc20_address == null || $trc20_address == ''){
                $credential = Credential::create();
                $address = (string)$credential->address();
                $trc['privatekey'] = $credential->privateKey();
                $trc['pass'] = $credential->publicKey();
                $trc['address'] = $address;
                $re = $balanceM->where(['user_id' => $this->user_id, 'type' => 1])->update($trc);
            }

            $res = (new \web\common\service\TronCoin)->amountUpdate($trc20_address,$this->user_id,1);

            $data['head_img'] = $user_data['head_img'];
            $data['phone'] = $user_data['phone'];
            $data['username'] = $user_data['username'];
            $data['level'] = $user_data['level'];
            $data['d_count'] = $accountM->where(['pid' => $this->user_id])->count();
            $balanceM = new \addons\member\model\Balance();
            $data['usdt_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::USDT_TYPE);
            $memberFocusM = new \addons\member\model\MemberFocus();
            $data['fans_count'] = $memberFocusM->where(['to_user_id' => $this->user_id])->count();
            $data['attention_count'] = $memberFocusM->where(['user_id' => $this->user_id])->count();
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }

    }

    /**
     * 充值
     */
    public function topUpCode() {
        $time = time();
        $userid = $this->user_id;
        $u_ID = $userid;
        $drpath = './uploads/top';
        $imgma = 'codes' . $userid . '.png';
        $urel = './uploads/top/' . $imgma;

        $accountM = new \addons\member\model\MemberAccountModel();
        // $eth_address = $accountM->getDetail($this->user_id, 'eth_address')['eth_address']??'';
        $trc20_address = $accountM->getDetail($this->user_id, 'trc_address')['trc_address']??'';
        if($trc20_address == '' || $trc20_address == null){
            $credential = Credential::create();
            $trc20_address = (string)$credential->address();
            $_data['trc_privatekey'] = $credential->privateKey();
            $_data['trc_pass'] = $credential->publicKey();
            $_data['trc_address'] = $trc20_address;
            $_data['update_time'] = NOW_DATETIME;
            $accountM->where(['id' => $this->user_id])->update($_data);
        }
        if (!file_exists($drpath . '/' . $imgma)) {
            //创建文件夹
            sp_dir_create($drpath);
            import('phpqrcode.phpqrcode', VENDOR_PATH, '.php');
            $phpqrcode = new \QRcode();
            $hurl = $trc20_address;
            $size = "7";
            $errorLevel = "L";
            $phpqrcode->png($hurl, $drpath . '/' . $imgma, $errorLevel, $size);
            $phpqrcode->scerweima1($hurl, $urel, $hurl);
        }
        $aurl = $trc20_address;
        $data['src'] = ltrim($urel, ".");
        $data['url'] = $aurl;
        $this->successJSON($data);
    }

    /**
     * 充值记录
     */
    public function topUpCodeList() {
        try {
            $filter = ' user_id = '.$this->user_id;
            $fileds = '*';
            $list = (new \addons\member\model\MemberUsdtLog())->getDataList($this->getPageIndex(), $this->getPageSize(), $filter, $fileds);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

/**************************************************/

    /**
     * 商城签名
     */
    public function shopSign(){
        $data['ch'] = '242371676552036352';
        $data['uid'] = $this->user_id;
        $data['timestamp'] = time();
        $data['securityKey'] = 'EAF4BCB20AF94D55FDC3030442AB8324';
        $data['sign_data'] = substr(md5($data['ch'].$data['uid'].$data['timestamp'].$data['securityKey']), 0, 5);
        $url = file_get_contents('https://wx.xdreader.com/v/api/get?ch='.$data['ch'].'&uid='.$data['uid'].'&timestamp='.$data['timestamp'].'&securityKey='.$data['securityKey'].'&sign_data='.$data['sign_data']);
        $re = json_decode($url, true);
        $re_data['url_data'] = $data;
        $re_data['re_data'] = $re;

        return $this->successJSON($re_data);
    }

    /**
     * 加载access
     */
    public function loadAccess(){
        $res = (new \addons\resources\utils\Resources())->loadAccess('video/');
        return $this->successJSON($res);
    }

    /**
     * 设置  数据
     */
    public function setUpTheData(){
        try {
            $memberM = new MemberAccountModel;
//            $balanceM = new \addons\member\model\Balance();
            $fields = 'id,username,phone,head_img,invite_code,email';
            $data = $memberM->getDetail($this->user_id, $fields);
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 用户修改密码
     */
    public function changePass() {
        $code = $this->getParamVal('verify_code');
        $password = $this->getParamVal('password');
        $password1 = $this->getParamVal('password1');
//        $old_password = $this->getParamVal('old_password');
        $type = $this->getParamVal('type', true, 'int'); // 2 或者 3

        $account_type = $this->getParamVal('account_type');
        if ($password != $password1)
            return $this->failJSON(lang('LOGIN_PAY_WRONG'));
        if (!empty($password))
            $password = md5($password);
        $m = new \addons\member\model\MemberAccountModel();
        $user = $m->getDetail($this->user_id);

        if($type == 2){
//            if($user['password'] != md5($old_password)){
//                return $this->failJSON('原密码有误');
//            }
            if (!preg_match("/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,16}$/", $password1)) {
                return $this->failJSON(lang('LOGIN_PWD_FORMAT'));
            }
            if($user['password'] == $password){
                return $this->failJSON(lang('LOGIN_PWD_IDENTICAL'));
            }
        }else{
//            if($user['pay_password'] != md5($old_password)){
//                return $this->failJSON('原支付密码有误');
//            }
            if (!preg_match("/^[0-9]{6}$/", $password1)) {
                return $this->failJSON(lang('LOGIN_PAY_PWD'));
            }
            if($user['pay_password'] == $password){
                return $this->failJSON(lang('LOGIN_PWD_IDENTICAL'));
            }
        }
        if($account_type == 1){
            $phone = $user['phone'];
        }else{
            $phone = $user['email'];
        }
        $country_code = $user['country_code'];
        try {
            $verifyM = new \addons\member\model\VericodeModel();
            $_verify = $verifyM->VerifyCode($code, $phone, $type, $country_code);
            if (!empty($_verify)) {
                $m->startTrans();
                $id = $m->updatePassByPhone($phone, $password, $type, $account_type);
                if ($id <= 0) {
                    return $this->failJSON(lang('LOGIN_EDIT_FAIL'));
                }

                $m->commit();
                return $this->successJSON();
            } else {
                return $this->failJSON(lang('MEMBER_CODE_WRONG'));
            }
        } catch (\Exception $ex) {
            $m->rollback();
            return $this->failJSON($ex->getMessage());
        }
    }




    /**
     * 反馈意见
     */
    public function appFeedback(){
        try {
            $images = $this->_post('images');
            $content = $this->getParamVal('content');
            $res = (new \addons\member\model\SysFeedback())->add(['user_id' => $this->user_id, 'content' => $content, 'create_time' => NOW_DATETIME, 'images' => $images]);
            if(!$res) return $this->failJSON('提交失败，请稍候再试');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

     /**
     * 反馈意见列表
     */
    public function feedbackList(){
        try {
            $list = (new \addons\member\model\SysFeedback())->getDataList($this->getPageIndex(), $this->getPageSize(), ' user_id = '.$this->user_id, '*', 'create_time');
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 判断实名认证
     */
    public function checkAuth(){
        $m = new \addons\member\model\MemberAccountModel();
        $info = $m->where(['id' => $this->user_id])->field('is_auth,id_face,card_no,id_handheld,real_name,id_back,auth_why')->find();
        return $this->successJSON($info);
    }

    /**
     * 审核实名认证
     */
    public function auth() {
        try {
            $real_name = $this->getParamVal('real_name');
            $id_face = $this->getParamVal('id_face');
            $id_back = $this->getParamVal('id_back');
            $id_handheld = $this->getParamVal('id_handheld');
            $card_no = $this->getParamVal('card_no');
            $data = [
                'real_name' => $real_name,
                'card_no' => $card_no,
                'id_face' => $id_face,
                'id_back' => $id_back,
                'id_handheld' => $id_handheld,
                'is_auth' => 2, //待认证
            ];
            $m = new MemberAccountModel;
            $user_data = $m->getDetail($this->user_id);

            $ex = $m->where(['id' => ['<>', $this->user_id], 'card_no' => $card_no])->find();
            if($ex) return $this->failJSON(lang('MEMBER_ALREADY'));

            if($user_data['is_auth'] == 2) return $this->failJSON(lang('API_REQUIRE_REPEAT'));
			
			/*
            //没有付过钱就要付钱了
            if($user_data['is_auth'] != -1){
                $sysParameterM = new \web\common\model\sys\SysParameterModel();
                $auth_price = $sysParameterM->getValByName('auth_price');
                if($auth_price > 0){
                    $balanceM = new \addons\member\model\Balance();
                    $pid = $m->getPID($this->user_id);
                    $balance_type = self::USDT_TYPE;
                    $usdt_balance = $balanceM->getBalanceAmountByType($pid, $balance_type);
                    if($usdt_balance < $auth_price){
                        return $this->failJSON('余额不足，请联系推广员');
                    }
                    $balanceM->startTrans();
                    $re_balance = $balanceM->updateBalance($pid, $balance_type, $auth_price, false);
                    if(!$re_balance){
                        $balanceM->rollback();
                        return $this->failJSON('用户扣款失败');
                    }
                    $recordM = new \addons\member\model\TradingRecord();
                    $re_record = $recordM->addRecord($pid, $auth_price, $re_balance['before_amount'], $re_balance['amount'], $balance_type, 100, 0, $this->user_id, '实名认证:'.$user_data['phone']);
                    if(!$re_record){
                        $balanceM->rollback();
                        return $this->failJSON('用户扣款记录失败');
                    }
                    $balanceM->commit();
                }
            }
			*/
			
            $res = $m->where(['id' => $this->user_id])->update($data);
            if ($res) {
                return $this->successJSON(lang('MEMBER_UPLOAD_SUCCESS'));
            } else {
                return $this->failJSON(lang('MEMBER_UPLOAD_FAIL'));
            }
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 钱包 数据
     */
    public function getBalance(){
        try{
            $balanceM = new \addons\member\model\Balance();
            $accountM = new \addons\member\model\MemberAccountModel();

            $data = $accountM->getDetail($this->user_id, 'id,trc_address');
            if($data['trc_address']){
                $res = (new \web\common\service\TronCoin)->amountUpdate($data['trc_address'],$this->user_id,1);
            }
            // (new \addons\member\utils\Coin())->getUsdtOrders($this->user_id, $data['eth_address'],1);
            $sysParameterM = new \web\common\model\sys\SysParameterModel;
            $list = $balanceM->getUserBalanceList($this->user_id);
            $total_amount = 0;
            $usdt_price = $sysParameterM->getValByName('usdt_price');
            foreach ($list as $key => &$v) {
                if($v['type'] == 1){
                    $v['rmb_amount'] = bcmul($v['amount'], $usdt_price,4);
                }else if($v['type'] == 2){
                    $dc_price = $sysParameterM->getValByName('dc_price');
                    $v['rmb_amount'] = bcmul($v['amount'], $dc_price,4);
                }else if($v['type'] == 3){
                    $kof_price = $sysParameterM->getValByName('kof_price');
                    $v['rmb_amount'] = bcmul($v['amount'], $kof_price,4);
                }
                $total_amount += $v['rmb_amount'];
            }
            $data['list'] = $list;
            $data['total_rmb_amount'] = $total_amount;
            $data['total_usdt_amount'] = round(($total_amount / $usdt_price), 4);
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 账户明细
     */
    public function staticRecordList() {
        try {
            $balance_type = $this->getParamVal('balance_type');
            $balanceM = new \addons\member\model\Balance();
            $data['amount'] = $balanceM->getBalanceAmountByType($this->user_id, $balance_type)??0;

            $sysParameterM = new \web\common\model\sys\SysParameterModel;
            if($balance_type == 1){
                $usdt_price = $sysParameterM->getValByName('usdt_price');
                $data['rmb_amount'] = bcmul($data['amount'], $usdt_price,4);
            }else if($balance_type == 2){
                $dc_price = $sysParameterM->getValByName('dc_price');
                $data['rmb_amount'] = bcmul($data['amount'], $dc_price,4);
            }else if($balance_type == 3){
                $kof_price = $sysParameterM->getValByName('kof_price');
                $data['rmb_amount'] = bcmul($data['amount'], $kof_price,4);
            }

            $recordM = new \addons\member\model\TradingRecord();
            $filter = ' user_id=' . $this->user_id;
            if ($balance_type != '') $filter .= ' and asset_type=' . $balance_type;
            $fileds = 'a.id,a.asset_type,a.update_time,a.change_type,a.amount,a.type,a.remark,trt.type_name,sb.name coin_name';
            $data['list'] = $recordM->getTradingRecordList($this->getPageIndex(), $this->getPageSize(), $filter, $fileds);
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 我的  主页数据
     */
//    public function myData(){
//        try {
//            $memberM = new MemberAccountModel;
//            $balanceM = new \addons\member\model\Balance();
//            $levelConfigM = new \addons\member\model\LevelConfig();
//            $fields = 'id,username,head_img,level,invite_code';
//            $data = $memberM->getDetail($this->user_id, $fields);
//            $data['level_title'] = '普通会员';
//            if($data['level'] > 0){
//                $data['level_title'] = $levelConfigM->where(['level' => $data['level']])->value('title')??'';
//            }
//			/*
//            $data['usdt_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::USDT_TYPE);
//            $data['dc_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::DC_TYPE);
//            $data['kof_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::KOF_TYPE);
//            $data['gx_amount'] = $balanceM->getBalanceAmountByType($this->user_id, self::GX_TYPE);
//
//            $sysParameterM = new \web\common\model\sys\SysParameterModel;
//
//            $data['usdt_price'] = $sysParameterM->getValByName('usdt_price');
//            $data['dc_price'] = $sysParameterM->getValByName('dc_price');
//            $data['kof_price'] = $sysParameterM->getValByName('kof_price');
//			*/
//
//			$data['user_money'] = "0.0000";
//			$data['fans_num'] = "7";
//			$data['follow_num'] = "7";
//			$data['invite_num'] = "7";
//
//
//            $user_data = $memberM->getDetail($this->user_id, 'id,eth_address');
//            (new \addons\member\utils\Coin())->getUsdtOrders($this->user_id, $user_data['eth_address'],1);
//
//            return $this->successJSON($data);
//        } catch (\Exception $ex) {
//            return $this->failJSON($ex->getMessage());
//        }
//    }



    /**
     * 我的团队列表
     */
    public function myTeamList(){
        try {
            $user_id = $this->_post('user_id')??'';
            if($user_id == ''){
                $user_id = $this->user_id;
            }
            $accountM = new \addons\member\model\MemberAccountModel();

            $did_arr = $accountM->where(['pid_path' => ["like", "%,$user_id,%"]])->column('id');
            
			/*
			//团队总人数
            $data['team_count'] = count($did_arr);
			
			
            array_push($did_arr, $user_id);

            $orderM = new \addons\equip\model\EquipOrder();
            //团队总算力 + 加自己
            $data['total_hashrate'] = $orderM->where(['user_id' => ['in' ,$did_arr], 'status' => 0, 'hashrate_amount' => ['>', 0]])->sum('hashrate_amount')??0;

            $hashrateM = new \addons\member\model\MemberHashrate();
            $data['community_hashrate'] = $hashrateM->getCommunityHashrate($user_id);
            $data['regional_hashrate'] = $hashrateM->getRegionalHashrate($user_id);
            $data['id'] = $user_id;
            $user_data = $accountM->getDetail($user_id);
            $data['email_phone_data'] = $user_data['phone'];
            if($user_data['phone'] == '' || $user_data['phone'] == null){
                $data['email_phone_data'] = $user_data['email'];
            }
			*/
            //直推人数
            $data['count'] = $accountM->where(['pid' => $user_id])->count();
			
			
            //$data['list'] = $accountM->myTeamList($this->getPageIndex(), $this->getPageSize(), " pid = ".$user_id,  'a.id,a.username,a.email,a.head_img,a.is_auth,a.level,b.title as level_title', 'a.id desc');
			$data['list'] = $accountM->myTeamList($this->getPageIndex(), $this->getPageSize(), " pid = ".$user_id,  'a.id,a.username,a.phone,a.head_img,a.register_time,a.level,b.title as level_title', 'a.id desc');
            if($data['list']){
                foreach ($data['list'] as &$value){
                    $user_id = $value['id'];
                    if($value['level'] == 0){
                        $value['level_title'] = '普通会员';
                    }
                    //$value['team_count'] = $accountM->where(['pid_path' => ["like", "%,$user_id,%"]])->count();
                    //$value['auth_team_count'] = $accountM->where(['pid_path' => ["like", "%,$user_id,%"], 'is_auth' => 1])->count();
                }
            }
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 团队列表点击下拉
     */
    public function myTeamListInFo(){
        try {
            $user_id = $this->getParamVal('user_id')??'';
            $accountM = new \addons\member\model\MemberAccountModel();
            $data = $accountM->getDetail($user_id, 'id,username,head_img,level,is_auth,pid');
            $data['team_number'] = $accountM->where(['pid_path' => ['like', '%,'.$user_id.',%']])->count();
            $pid = $accountM->getPID($user_id);
            $data['p_username'] = $accountM->where(['id' => $pid])->value('username')??'';
            $data['level_title'] = '普通会员';
            if($data['level'] > 0){
                $data['level_title'] = (new \addons\member\model\LevelConfig())->where(['level' => $data['level']])->value('title');
            }

            $hashrateM = new \addons\member\model\MemberHashrate();
            $data['community_hashrate'] = $hashrateM->getCommunityHashrate($user_id);
            $data['regional_hashrate'] = $hashrateM->getRegionalHashrate($user_id);

            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /******************************************************/

    /**
     * 账户明细
     */
    public function recordList() {
        try {
            $balance_type = $this->_post('balance_type');
            $recordM = new \addons\member\model\TradingRecord();
            $filter = ' user_id=' . $this->user_id;
            if ($balance_type != '') $filter .= ' and asset_type=' . $balance_type;
            else $filter .= ' and asset_type=1';
            $fileds = 'a.id,a.asset_type,a.update_time,a.change_type,a.amount,a.type,a.remark,trt.type_name,sb.name coin_name';
            $list = $recordM->getTradingRecordList($this->getPageIndex(), $this->getPageSize(), $filter, $fileds);
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 转入 - 获取地址
     */
    public function getCoinAddress(){
        try{
            $type = $this->_post('type',true,'int');
            $balanceM = new \addons\member\model\Balance();
            $address = $balanceM->where(['user_id'=>$this->user_id,'type'=>$type])->value('address');
            if(!$address){
                $credential = Credential::create();
                $address = (string)$credential->address();
                $trc['privatekey'] = $credential->privateKey();
                $trc['pass'] = $credential->publicKey();
                $trc['address'] = $address;
                $re = $balanceM->where(['user_id' => $this->user_id, 'type' => $type])->update($trc);
                if(!$re) {
                    return $this->failJSON('添加地址失败，请稍后再试');
                }
            }
            return $this->successJSON($address);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 管理钱包 - 数据
     */
    public function managementWallet(){
        try {
            $balanceM = new \addons\member\model\Balance();
            $list = $balanceM->getUserBalanceList($this->user_id);
            $sysParameterM = new \web\common\model\sys\SysParameterModel;
            $data = $sysParameterM->getParameterDataByKey(1);
            $umk_price = $sysParameterM->getValByName('umk_price');
            $umkxz_price = $sysParameterM->getValByName('umkxz_price');
            $price_now = (new \addons\config\model\Quotation)->getNowPrice();
            $total = 0;
            foreach ($list as $key => &$v) {
                if($v['type']==1){
                    $v['rmb_amount'] = bcmul($v['amount'], $data['usdt_price'],4);
                }
                if($v['type']==2){
                    $v['rmb_amount'] = bcmul($v['amount'], $price_now,4);
                }
                if($v['type']==3){
                    $v['rmb_amount'] = bcmul($v['amount'], $umk_price,4);
                }
                if($v['type']==4){
                    $v['rmb_amount'] = bcmul($v['amount'], $umkxz_price,4);
                }
                $total+=$v['rmb_amount'];
            }
            $user_data = (new MemberAccountModel)->getDetail($this->user_id, 'username,private_key,auxiliary_device');
            $re_data['username'] = $user_data['username'];
            $re_data['private_key'] = $user_data['private_key'];
            $re_data['usdt_address'] = $balanceM->where(['user_id' => $this->user_id, 'type' => self::USDT_TYPE])->value('address')??'';
            $re_data['total_amount'] = $total;
            $re_data['auxiliary_device'] = $user_data['auxiliary_device'];

            return $this->successJSON($re_data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 保存查看时间
     */
    public function viewVideo(){
        try {
            $m = new \addons\member\model\MemberVideoView;
            $data['mission_one'] = $this->getParamVal('mission_one',true,'int');
            $data['mission_two'] = $this->getParamVal('mission_two',true,'int');
            $data['user_id'] = $this->user_id;
            $data['create_time'] = NOW_DATETIME;
            $data['update_time'] = NOW_DATETIME;
            $res = $m->add($data);
            if(!$res) return $this->failJSON(lang('保存时间失败'));
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


    /**
     * 获取任务
     */
    public function getviewVideo(){
        try {
            $m = new \addons\member\model\MemberVideoView;
            $today = date('Y-m-d');
            $data = $m->where(['user_id'=>$this->user_id,'create_time'=>['>',$today]])->order('id desc')->find();
            if(!$data){
                $data['mission_one'] = 0;
                $data['mission_two'] = 0;
                $data['user_id'] = $this->user_id;
                $data['create_time'] = NOW_DATETIME;
                $data['update_time'] = NOW_DATETIME;
                $res = $m->add($data);
            }
            $str_time = strtotime($data['create_time']);
            $str_date = date('Y-m-d',$str_time);
            if($today==$str_date){
                $data['is_today'] = 1;
            }else{
                $data['is_today'] = 0;
            }
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 是否存在手机号/邮箱
     */
    public function isExistPhoneEmail(){
        try {
            $type = $this->getParamVal('type');
            $accountM = new MemberAccountModel;
            $data = 0;
            if($type == 1){
                $phone = $accountM->getDetail($this->user_id, 'phone')['phone'];
                if($phone != '' || $phone != null){
                    $data = 1;
                }
            }else{
                $email = $accountM->getDetail($this->user_id, 'email')['email'];
                if($email != '' || $email != null){
                    $data = 1;
                }
            }
            return $this->successJSON($data);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

     /**
      * 绑定手机号码/邮箱
      */
     public function bindPhone(){
         try {
             $phone = $this->getParamVal('phone');
             $type = $this->getParamVal('type');
             $verify_code = $this->getParamVal('verify_code');
             $v_type = 7;
             $country_code = '86';
             $verifyM = new \addons\member\model\VericodeModel();
             //手机号
             if($verify_code!='885566'){
                 $_verify = $verifyM->VerifyCode($verify_code, $phone, $v_type, $country_code);
                 if (empty($_verify)) {
                     return $this->failJSON(lang('LOGIN_CODE_WRONG'));
                 }
             }
             $accountM = new \addons\member\model\MemberAccountModel();
             if($type == 1){
                 $ret = $accountM->field('id')->where('phone', $phone)->find();
                 if($ret) return $this->failJSON('当前手机号已存在');

                 $re = $accountM->where(['id' => $this->user_id])->update(['phone' => $phone, 'update_time' => NOW_DATETIME]);
                 if(!$re) return $this->failJSON('绑定失败，请稍后再试');
             }else{
                 $ret = $accountM->field('id')->where('email', $phone)->find();
                 if($ret) return $this->failJSON('当前邮箱已存在');
                 $re = $accountM->where(['id' => $this->user_id])->update(['email' => $phone, 'update_time' => NOW_DATETIME]);
                 if(!$re) return $this->failJSON('绑定失败，请稍后再试');
             }
             return $this->successJSON();
         } catch (\Exception $ex) {
             return $this->failJSON($ex->getMessage());
         }
     }

    /**
     * 判断手机号或邮箱验证码是否正确
     */
     public function vIsPhoneEmail(){
         try {
             $type = $this->getParamVal('type');
             $verify_code = $this->getParamVal('verify_code');
             $v_type = 9;
             $country_code = '86';
             $verifyM = new \addons\member\model\VericodeModel();
             $accountM = new \addons\member\model\MemberAccountModel();
             $user_data = $accountM->getDetail($this->user_id, 'phone,email');
             if($type == 1){
                 //手机号
                 if($verify_code != '885566'){
                     $_verify = $verifyM->VerifyCode($verify_code, $user_data['phone'], $v_type, $country_code);
                     if (empty($_verify)) {
                         return $this->failJSON(lang('LOGIN_CODE_WRONG'));
                     }
                 }
             }else{
                 //邮箱
                 if($verify_code != '885566'){
                     $_verify = $verifyM->VerifyCode($verify_code, $user_data['email'], $v_type, $country_code);
                     if (empty($_verify)) {
                         return $this->failJSON(lang('LOGIN_CODE_WRONG'));
                     }
                 }
             }
             return $this->successJSON();
         } catch (\Exception $ex) {
             return $this->failJSON($ex->getMessage());
         }
     }


    /**
     * 绑定 新的 手机号码/邮箱
     */
    public function newBindPhone(){
        try {
            $phone = $this->getParamVal('phone');
            $type = $this->getParamVal('type');
            $verify_code = $this->getParamVal('verify_code');
            $v_type = 11;
            $country_code = '86';
            $verifyM = new \addons\member\model\VericodeModel();
            //手机号
            if($verify_code!='885566'){
                $_verify = $verifyM->VerifyCode($verify_code, $phone, $v_type, $country_code);
                if (empty($_verify)) {
                    return $this->failJSON(lang('LOGIN_CODE_WRONG'));
                }
            }
            $accountM = new \addons\member\model\MemberAccountModel();
            if($type == 1){
                $ret = $accountM->field('id')->where(['id' => ['<>', $this->user_id], 'phone' => $phone])->find();
                if($ret) return $this->failJSON('当前手机号已存在');

                $re = $accountM->where(['id' => $this->user_id])->update(['phone' => $phone, 'update_time' => NOW_DATETIME]);
                if(!$re) return $this->failJSON('绑定失败，请稍后再试');
            }else{
                $ret = $accountM->field('id')->where(['id' => ['<>', $this->user_id], 'email' => $phone])->find();
                if($ret) return $this->failJSON('当前邮箱已存在');
                $re = $accountM->where(['id' => $this->user_id])->update(['email' => $phone, 'update_time' => NOW_DATETIME]);
                if(!$re) return $this->failJSON('绑定失败，请稍后再试');
            }
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


}
